package pe.edu.upc.dsd.l33t.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pe.edu.upc.dsd.l33t.dao.BookDao;
import pe.edu.upc.dsd.l33t.model.Book;
import pe.edu.upc.dsd.l33t.service.BookService;

import com.google.gson.Gson;

@Path("/books")
@Service
public class BookServiceImpl implements BookService {
	
	private Logger logger = Logger.getLogger(BookServiceImpl.class);
	
	@Autowired
	private BookDao bookDao;

	@GET
	@Path("/{isbn}")
	@Produces("text/plain")
	@Consumes("text/plain")
	public Response find(@PathParam("isbn") String isbn){
		
		Book book;
		Gson gson = new Gson();
						
		System.out.println("isbn: " + isbn);
		
		book = bookDao.find(isbn);
		
		if (book == null) {
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity("El libro no existe").build();
		}
		
		return Response.ok(gson.toJson(book)).build();
	}

	@POST
	@Override
	public Response save(String json) {
		
		Gson gson = new Gson();
		Book book = gson.fromJson(json, Book.class);
		
		logger.debug(book.getTitle());
		
		try {
			bookDao.save(book);
			logger.debug("book saved");
		} catch (Exception e) {
			logger.error("Error al guardar el libro", e);
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity("Error al guardar el libro").build();
		}
				
		return Response.ok(gson.toJson(book)).build();
	}

	@GET
	@Path("/hot")
	@Produces("text/plain")
	@Consumes("text/plain")
	@Override
	public Response getFiveLatestBooks() {		
		
		List<Book> bookList = bookDao.getFiveLatestBooks();
		
		Gson gson = new Gson();
		
		if (bookList == null | bookList.size() == 0) {
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity("No existen libros").build();
		}
		
		return Response.ok(gson.toJson(bookList)).build();
	}

	@GET
	@Path("/title/{title}")
	@Produces("text/plain")
	@Consumes("text/plain")
	@Override
	public Response getBooksByTitle(@PathParam("title") String title) {

		List<Book> bookList = bookDao.getBooksByTitle(title);
		
		Gson gson = new Gson();
		
		if (bookList == null | bookList.size() == 0) {
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity("No existen libros con titulo '" + title + "'").build();
		}
		
		return Response.ok(gson.toJson(bookList)).build();
		
	}

	@GET
	@Path("/author/{author}")
	@Produces("text/plain")
	@Consumes("text/plain")
	@Override
	public Response getBooksByAuthor(@PathParam("author") String author) {

		List<Book> bookList = bookDao.getBooksByAuthor(author);
		
		Gson gson = new Gson();
		
		if (bookList == null | bookList.size() == 0 ) {
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity("No existen libros del autor '" + author + "'").build();
		}
		
		return Response.ok(gson.toJson(bookList)).build();
	}

	@GET
	@Path("/category/{category}")
	@Produces("text/plain")
	@Consumes("text/plain")
	@Override
	public Response getBooksByCategory(@PathParam("category") String category) {

		List<Book> bookList = bookDao.getBooksByCategory(category);
		
		Gson gson = new Gson();
		
		if (bookList == null | bookList.size() == 0 ) {
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity("No existen libros del autor '" + category + "'").build();
		}
		
		return Response.ok(gson.toJson(bookList)).build();
	}
	
	

}
